package com.liuym.admin.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.liuym.admin.repository.model.vo.ClassVO;
import com.liuym.admin.service.TableNameService;
import com.liuym.admin.util.ClassScoreTableNameUtil;
import com.liuym.common.annotation.AdminLogin;
import com.liuym.common.constants.Constants;
import com.liuym.common.constants.GradeResponse;
import com.liuym.common.dubbo.classes.api.ClassScoreDubboService;
import com.liuym.common.dubbo.classes.api.ClassesDubboService;
import com.liuym.common.dubbo.classes.dto.ClassDTO;
import com.liuym.common.dubbo.classes.dto.ClassScoreDTO;
import com.liuym.common.exception.ClassException;
import com.vip.vjtools.vjkit.mapper.BeanMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Liuym
 * @date 2018/12/10 13:39
 * @describe：
 */
@AdminLogin
@RestController
@RequestMapping("/admin/class")
public class ClassController {

    private static Logger logger = LoggerFactory.getLogger(ClassController.class);

    @Reference(version = "${class.dubbo.version}")
    private ClassScoreDubboService classScoreDubboService;
    @Reference(version = "${class.dubbo.version}")
    private ClassesDubboService classDubboService;
    @Autowired
    private TableNameService tableNameService;

    @PutMapping("/resetClass")
    public GradeResponse resetClass() {
        try {

            ClassScoreTableNameUtil.setClassScoreTableName(ClassScoreTableNameUtil.getClassScoreTableName());
            classDubboService.updateClassScoreTo80();
            try {
                classScoreDubboService.addClassScoreTable(ClassScoreTableNameUtil.getClassScoreTableName().toString());
            } catch (ClassException e) {
                logger.warn("请不要重复操作！", e);
                return GradeResponse.fail(e.getCode(), e.getMsg());
            } finally {
                ClassScoreTableNameUtil.getClassScoreTableName().delete(13, ClassScoreTableNameUtil.getClassScoreTableName().length());
            }

        } catch (Exception e) {
            logger.error("调用updateClassScoreTo80失败！", e);
            return GradeResponse.internalServerError();
        }
        return GradeResponse.success();
    }

    @PostMapping("/addClass")
    public GradeResponse addClass(ClassDTO classDTO) {
        int result = 0;
        try {
            result = classDubboService.addClass(classDTO);
        } catch (Exception e) {
            if (e instanceof ClassException) {
                logger.warn("warn", e);
                return GradeResponse.fail(((ClassException) e).getCode(), ((ClassException) e).getMsg());
            } else {
                logger.error("error", e);
                return GradeResponse.internalServerError();
            }
        }
        return GradeResponse.success(result);
    }

    @DeleteMapping("/deleteClass")
    public GradeResponse deleteClass(long classNumber) {
        int result = 0;
        try {
            result = classDubboService.deleteClass(classNumber);
        } catch (ClassException e) {
            logger.error("error", e);
            return GradeResponse.internalServerError();
        }
        return GradeResponse.success(result);
    }

    @PutMapping("/updateClass")
    public GradeResponse uptateClass(ClassDTO classDTO) {
        int result = 0;
        try {
            result = classDubboService.updateClass(classDTO);
        } catch (Exception e) {
            if (e instanceof ClassException) {
                logger.warn("warn", e);
                return GradeResponse.fail(((ClassException) e).getCode(), ((ClassException) e).getMsg());
            }
            logger.error("error", e);
            return GradeResponse.internalServerError();
        }
        return GradeResponse.success(result);
    }

    @GetMapping("/findClassByClassNumber")
    public GradeResponse findClassByClassNumber(long classNumber) {
        ClassVO classVO = null;
        try {
            ClassDTO classDTO = classDubboService.getClassByNumber(classNumber);
            if (classDTO!=null) {
                classVO = BeanMapper.map(classDTO, ClassVO.class);
            }
            List<ClassScoreDTO> classScoreByClassNumber = classScoreDubboService.getClassScoreByClassNumber(tableNameService.findByTableAndTime(Constants.CLASS_SCORE_TABLE_NAME).getTableName(),
                    classNumber);
            classVO.setClassScoreList(classScoreByClassNumber);
        } catch (ClassException e) {
            logger.error("error", e);
            return GradeResponse.internalServerError();
        }
        return GradeResponse.success(classVO);
    }

    @GetMapping("/findAllClass")
    public GradeResponse findAllClass() {
        List<ClassDTO> allClass = null;
        try {
            allClass = classDubboService.getAllClass();
        } catch (ClassException e) {
            logger.error("error", e);
            return GradeResponse.internalServerError();
        }
        return GradeResponse.success(allClass);
    }

    @GetMapping("/findAllClassNumber")
    public GradeResponse findAllClassNumber() {
        List<ClassDTO> allClass = null;
        Set<Integer> set = new HashSet();
        try {
            allClass = classDubboService.getAllClass();
            allClass.stream().forEach(classDTO -> {
                set.add(classDTO.getYear());
            });
        } catch (ClassException e) {
            logger.error("error", e);
            return GradeResponse.internalServerError();
        }
        return GradeResponse.success(set);
    }

    @GetMapping("/findClassByYear")
    public GradeResponse findClassByYear(Integer year) {
        List<ClassDTO> allClass = null;
        Set<ClassDTO> set = new HashSet();
        try {
            allClass = classDubboService.getAllClass();
            allClass.stream().forEach(classDTO -> {
                if (year.equals(classDTO.getYear())) {
                    set.add(classDTO);
                }
            });
        } catch (ClassException e) {
            logger.error("error", e);
            return GradeResponse.internalServerError();
        }
        return GradeResponse.success(set);
    }
}
